package ws.performancetest.tests.synchronization.lock;

/**
 * Abstract implementation of the shared memory.
 *
 * This class has some common properties of the concrete implementations.
 * 
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public abstract class AbstractSharedListImpl implements SharedList {

  /**
   * Constant value that represents the id of the lock owner, when there's
   * no lock owner.
   */
  protected final static int LOCKED_BY_NONE = -9;
  /**
   * This {@code int} actually represents the list.
   *
   * There will be no real data manipulation, when {@code add} is called
   * this value is incremented and when {@code remove} is called this value is
   * decremented.
   */
  protected int list;
  /**
   * Shared list maximum size.
   */
  protected int MAX_SIZE;
  /**
   * Tells if the shared memory is locked by a thread.
   */
  protected boolean locked = false;
  /**
   * Id of the thread that owns the lock.
   */
  protected long lockedBy = LOCKED_BY_NONE;

  /**
   * Create a new instance.
   */
  public AbstractSharedListImpl() {
    this.list = 0;
    this.MAX_SIZE = 1;
  }
  
  /**
   * Create a new instance.
   *
   * @param size Size of the shared list
   * @param max Shared list maximum size.
   */
  public AbstractSharedListImpl(int size, int max) {
    this.list = size;
    this.MAX_SIZE = max;
  }

  /**
   * For now, this operation will not be implemented.
   */
  public void lock() {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /**
   * For now, this operation will not be implemented.
   */
  public void unlock() {
    throw new UnsupportedOperationException("Not supported yet.");
  }

  /**
   * Check the object's invariant.
   *
   * Each time the object is used, it may be on one legal state, so after
   * it's manipulation is important for its consistency.
   *
   * @return invariant condition check. True if invariant is ensured.
   */
  protected boolean invariant() {
    return list >= 0 && list <= MAX_SIZE;
  }
}
